﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DesktopGarden.Classes;
using Microsoft.Win32;
using System.IO;
using System.Drawing.Imaging;

namespace DesktopGarden.Windows
{
    /// <summary>
    /// Interaction logic for FlowerEditor.xaml
    /// </summary>
    public partial class FlowerEditor : Window
    {
        public bool isFlower = false;
        public bool isNewPlant = false;
        int[] zones = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        Plant CurrentPlant;
        FlowerInstance CurrentFlower;


        public FlowerEditor(FlowerInstance flower, bool Editable = true)
        {
            InitializeComponent();
            CurrentFlower = flower;
            SetPlant();
            ComboBoxLoads();
            if (!Editable)
                btnSave.Visibility = System.Windows.Visibility.Collapsed;
            FlowerChoosen();
        }

        private void FlowerChoosen()
        {
            if (!isFlower)
                gFlower.Visibility = System.Windows.Visibility.Collapsed;
            else
                gFlower.Visibility = System.Windows.Visibility.Visible;
        }

        public void SetPlant()
        {
            cmbbxPlant.SelectedItem = CurrentFlower.PlantType;
            txtPlantName.Text = CurrentFlower.Name;
            cmbbxGStart.SelectedItem = CurrentFlower.GrowingPeriodStart;
            cmbbxGEnd.SelectedItem = CurrentFlower.GrowingPeriodEnd;
            cmbbxRegionStart.SelectedItem = CurrentFlower.StartRegion;
            cmbbxRegionStop.SelectedItem = CurrentFlower.EndRegion;
            cmbbxWater.SelectedItem = CurrentFlower.Waterlevel;
            cmbbxSun.SelectedItem = CurrentFlower.Sunlight;
            txtPlanHeight.Text = Convert.ToString(CurrentFlower.Height);
            txtPurchasedFrom.Text = CurrentFlower.BoughtFrom;
            dtDatePurchased.DisplayDate = CurrentFlower.DateBought;
            txtNumberPlanted.Text = Convert.ToString(CurrentFlower.NumberPlanted);
            dtpkDatePlanted.DisplayDate = CurrentFlower.DatePlanted;
            txtDesc.Text = CurrentFlower.Description;
            txtNotes.Text = CurrentFlower.Notes;
            txtFlowerColor.Text = CurrentFlower.FlowerColor;
            cmbxLifeCycle.SelectedItem = CurrentFlower.PlantLifeCycle;
            cmbxProduction.SelectedItem = CurrentFlower.BloomProductionQuality;
            txtFlowerType.Text = CurrentFlower.TypeofFlower;
            
            if(CurrentFlower.Color !=null)
                cmbbxKey.SelectedColor = ((SolidColorBrush)CurrentFlower.Color).Color;

            if (CurrentFlower.Image != null)
                LoadPicture();

        }
        public void ComboBoxLoads()
        {
            cmbbxWater.Items.Add(Plant.HIGHWATER);
            cmbbxWater.Items.Add(Plant.AVGWATER);
            cmbbxWater.Items.Add(Plant.LOWWATER);
            cmbbxSun.Items.Add(Plant.FULLSUN);
            cmbbxSun.Items.Add(Plant.PARSUN);
            cmbbxSun.Items.Add(Plant.NOSUN);
            cmbbxRegionStart.ItemsSource = zones;
            cmbbxRegionStop.ItemsSource = zones;
            cmbbxPlant.Items.Add(Plant.VEG);
            cmbbxPlant.Items.Add(Plant.FRUIT);
            cmbbxPlant.Items.Add(Plant.FLOWER);
            cmbbxGStart.Items.Add(Plant.EARLYSPRING);
            cmbbxGStart.Items.Add(Plant.SPRING);
            cmbbxGStart.Items.Add(Plant.SUMMER);
            cmbbxGStart.Items.Add(Plant.FALL);
            cmbbxGEnd.Items.Add(Plant.EARLYSPRING);
            cmbbxGEnd.Items.Add(Plant.SPRING);
            cmbbxGEnd.Items.Add(Plant.SUMMER);
            cmbbxGEnd.Items.Add(Plant.FALL);
            cmbxLifeCycle.Items.Add(Plant.Annual);
            cmbxLifeCycle.Items.Add(Plant.Perennial);
            cmbxProduction.Items.Add(Plant.PRODGOOD);
            cmbxProduction.Items.Add(Plant.PRODAVG);
            cmbxProduction.Items.Add(Plant.PRODPOOR);
            dtDatePurchased.DisplayDate = DateTime.Today;
            dtpkDatePlanted.DisplayDate = DateTime.Today;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SavePlant();
            App.SaveFlowerInCatalog(CurrentFlower);
            this.DialogResult = true;
        }

        private void SavePlant()
        {

                CurrentFlower.PlantType = Convert.ToString(cmbbxPlant.SelectedItem);
                CurrentFlower.Name = txtPlantName.Text;
                CurrentFlower.GrowingPeriodStart = Convert.ToString(cmbbxGStart.SelectedItem);
                CurrentFlower.GrowingPeriodEnd = Convert.ToString(cmbbxGEnd.SelectedItem);
                CurrentFlower.StartRegion = Convert.ToInt16(cmbbxRegionStart.SelectedItem);
                CurrentFlower.EndRegion = Convert.ToInt16(cmbbxRegionStop.SelectedItem);
                CurrentFlower.Waterlevel = Convert.ToString(cmbbxWater.SelectedItem);
                CurrentFlower.Sunlight = Convert.ToString(cmbbxSun.SelectedItem);
                CurrentFlower.Height = Convert.ToInt16(txtPlanHeight.Text);
                CurrentFlower.BoughtFrom = txtPurchasedFrom.Text;
                CurrentFlower.DateBought = dtDatePurchased.DisplayDate;
                CurrentFlower.NumberPlanted = Convert.ToInt16(txtNumberPlanted.Text);
                CurrentFlower.DatePlanted = dtpkDatePlanted.DisplayDate;
                CurrentFlower.Description = txtDesc.Text;
                CurrentFlower.Notes = txtNotes.Text;
                CurrentFlower.PlantLifeCycle = Convert.ToString(cmbxLifeCycle.SelectedItem);
                CurrentFlower.BloomProductionQuality = Convert.ToString(cmbxProduction.SelectedItem);
                CurrentFlower.SetColor(new SolidColorBrush(cmbbxKey.SelectedColor));
                CurrentFlower.FlowerColor = txtFlowerColor.Text;
                CurrentFlower.TypeofFlower = txtFlowerType.Text;

        }

        private void btnAddPic_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if ((bool)open.ShowDialog())
            {
                CurrentFlower.SetImage(open.FileName);
                LoadPicture();
            }
        }

        private void LoadPicture()
        {
            var bitmap = new System.Windows.Media.Imaging.BitmapImage();
            bitmap.BeginInit();
            MemoryStream memoryStream = new MemoryStream();
            // Save to a memory stream...
            CurrentFlower.Image.Save(memoryStream, ImageFormat.Bmp);
            // Rewind the stream...
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
            bitmap.StreamSource = memoryStream;
            bitmap.EndInit();

            imgPlant.Source = bitmap;
        }

        private void cmbbxPlant_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((string)cmbbxPlant.SelectedItem == Plant.FLOWER)
            {
                isFlower = true;
            }
            else
            {
                isFlower = false;
            }
            FlowerChoosen();
        }

        private void ColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            if (ColorExists())
                lblColorTaken.Visibility = System.Windows.Visibility.Visible;
            else
                lblColorTaken.Visibility = System.Windows.Visibility.Hidden;
        }

        private bool ColorExists()
        {
            return (from c in App.CurrentGarden.Flowers
                    where c.Color.ToString() == cmbbxKey.SelectedColor.ToString() && c.ID != CurrentFlower.ID
                    select c).FirstOrDefault() != null;
        }

    }

}